import pyautogui
import pyperclip

# 保护措施，避免失控
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.5


# 为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。
def gui_to_pause(t=0.5):
    pyautogui.PAUSE = t


# 切换到桌面
def go_to_win_desktop():
    pyautogui.hotkey('win', 'd')


# 使用按键
def gui_use_key_press(btn_name):
    pyautogui.press(btn_name)


# 获取特征图 在屏幕中的位置
def get_position_in_window(features):
    # 在当前屏幕中查找指定图片(图片需要由系统截图功能截取的图)
    win_position = None
    for i in range(10):
        win_position = pyautogui.locateOnScreen(features, confidence=0.9)
        if win_position:
            break
        else:
            continue
    # 获取定位到的图中间点坐标
    return win_position


# 获取特征图 在屏幕中的位置 list
def get_position_in_window_list(features):
    # 在当前屏幕中查找指定图片(图片需要由系统截图功能截取的图)
    list_win_position = None
    for i in range(10):
        list_win_position = pyautogui.locateAllOnScreen(features, confidence=0.9)
        if list_win_position:
            break
        else:
            continue
    # 获取定位到的图中间点坐标
    return list_win_position


# 鼠标双击
def mouse_double_click(x, y):
    pyautogui.doubleClick(x, y)


# 鼠标单击
def mouse_left_click(x, y):
    pyautogui.leftClick(x=x + 10, y=y + 10)


# 输入文字
def gui_paste(foo):
    pyperclip.copy(foo)
    pyautogui.hotkey('ctrl', 'v')


def clip_copy(foo):
    pyperclip.copy(foo)


# 复制取值
def gui_copy():
    pyautogui.hotkey('ctrl', 'c')
    return pyperclip.paste()


# 全选
def gui_select_all():
    pyautogui.hotkey('ctrl', 'a')


# 关闭当前页
def gui_close_page():
    pyautogui.hotkey('alt', 'f4')


# 鼠标移动
def mouse_move(x=0, y=0):
    pyautogui.moveTo(x=x, y=y)


# 获取当前鼠标位置
def mouse_get_position():
    currentMouseX, currentMouseY = pyautogui.position()
    return currentMouseX, currentMouseY


def mouse_reset():
    mouse_move()


# 保存页面截图
def gui_window_img():
    pyautogui.screenshot()


def get_window_size():
    return pyautogui.size()  # 屏幕的宽度和高度


# 返回中心
def get_img_center(win_position):
    return pyautogui.center(win_position)


# 单击 center
def mouse_center_click(center):
    pyautogui.click(center)


# 页面滑动
def scroll_page(amount):
    pyautogui.scroll(amount)


# 组合按键
def gui_hotkey(btn1, btn2):
    pyautogui.hotkey(btn1, btn2)


# 鼠标按下
def mouse_down():
    pyautogui.mouseDown()


# 鼠标释放
def mouse_up():
    pyautogui.mouseUp()


# 鼠标按住移动
def mouse_drag_to(x, y):
    pyautogui.dragTo(x, y)


# 根据窗口名称  获取窗口
def get_win_with_title(win_title):
    return pyautogui.getWindowsWithTitle(win_title)


# 获取所有的窗口
def get_all_win():
    return pyautogui.getAllWindows()


# 获取激活窗口的标题
def get_active_win_title():
    pyautogui.getAllWindows
    return pyautogui.getActiveWindowTitle()


# 获取所有窗口的 标题
def get_all_titles():
    return pyautogui.getAllTitles()


# 窗口关闭
def win_to_close(a_win):
    a_win.close()


# 窗口最小化
def win_to_minimize(a_win):
    a_win.minimize()


# 激活窗口
def win_to_activate(a_win):
    a_win.activate()


# 恢复窗口
def win_to_restore(a_win):
    a_win.restore()


# 消息提示框
def gui_msg_box(msg, title='程序异常'):
    pyautogui.alert(text=msg, title=title, button='确定')
